/*
	This file is part of AVRCppLib.

    AVRCppLib is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    AVRCppLib is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with AVRCppLib.  If not, see <http://www.gnu.org/licenses/>.

    Copyright (C) Maxime Lemonnier 2007-2011
 */

#define USI_COUNTER_MASK 0x0F	//0b00001111
#define USI_CLOCK_SOURCE_MASK 0x0C //0b00001100
bool USIHardware::readStartConditionInterruptFlag()
{
	return bitIsSetInRegister(*usisr, USISIF);

}
void USIHardware::clearStartConditionInterruptFlag()
{
	setBit(*usisr, USISIF);
}

void USIHardware::enableStartConditionInterrupts()
{
	setBit(*usicr, USISIE);
}

void USIHardware::disableStartConditionInterrupts()
{
	clearBit(*usicr, USISIE);
}

bool USIHardware::readCounterOverflowInterruptFlag()
{
	return bitIsSetInRegister(*usisr, USIOIF);
}

void USIHardware::clearCounterOverflowInterruptFlag()
{
	setBit(*usisr, USIOIF);
}

void USIHardware::enableCounterOverflowInterrupts()
{
	setBit(*usicr, USIOIE);
}

void USIHardware::disableCounterOverflowInterrupts()
{
	clearBit(*usicr, USIOIE);
}
bool USIHardware::readStopConditionFlag()
{
	return bitIsSetInRegister(*usisr, USISIF);
}

void USIHardware::clearStopConditionFlag()
{
	setBit(*usisr, USIPF);
}

bool USIHardware::readWriteCollisionFlag()
{
	return bitIsSetInRegister(*usisr, USISIF);
}
uint8_t USIHardware::readCounterValue()
{
	return *usisr & USI_COUNTER_MASK;
}
void USIHardware::disable()
{
	clear2Bits(*usicr, USIWM1, USIWM0);

}
void USIHardware::enableInSPIMode()
{
	clearBit(*usicr, USIWM1);
	setBit(*usicr, USIWM0);
}

void USIHardware::enableInI2CMode()
{
	setBit(*usicr, USIWM1);
	setBit(*usicr, USIWM0);
}

void USIHardware::setClockSourceMode(ClockSource clockSource)
{
	*usicr |= USI_CLOCK_SOURCE_MASK & clockSource << 2;

}
void USIHardware::setClockStrobe()
{
	setBit(*usicr, USICLK);

}
void USIHardware::clearClockStrobe()
{
	clearBit(*usicr, USICLK);
}
void USIHardware::toggleClock()
{
	setBit(*usicr, USITC);
}

uint8_t USIHardware::readByte()
{
	return *usibr;
}

void USIHardware::writeByte(uint8_t byte)
{
	*usidr = byte;
}
